package com.hx.mypetstore.ui.order;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.hx.mypetstore.Bean.OrderBean;
import com.hx.mypetstore.R;
import com.hx.mypetstore.constant.OrderState;

import java.util.List;

public class OrderAdapter extends BaseAdapter {

    private CheckInterface checkInterface;
    private Handler mHandler;
    private Context mContext;
    private List<OrderBean> menuDatas;
    private int selectedPos = -1;
    private String mClickedItemName = null;


    public OrderAdapter(Context mContext, List<OrderBean> menuDatas) {
        this.mContext = mContext;
        this.menuDatas = menuDatas;
    }
    /**
     * 按钮接口
     * @param checkInterface
     */
    public void setCheckInterface(CheckInterface checkInterface) {
        this.checkInterface = checkInterface;
    }

    @Override
    public int getCount() {
        if (menuDatas == null) {
            return 0;
        }
        return menuDatas.size();
    }

    @Override
    public Object getItem(int position) {
        if (menuDatas == null) {
            return null;
        }
        return menuDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final  ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_order, parent, false);
            holder = new OrderAdapter.ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (OrderAdapter.ViewHolder) convertView.getTag();
        }

        final  OrderBean order = menuDatas.get(position);

        if (order.getState().equals(OrderState.NOAPPLY.toString())){
            holder.btn_common.setVisibility(View.VISIBLE);
            holder.btn_common.setText("支付");
            holder.btn_common_more.setVisibility(View.VISIBLE);
            holder.btn_common_more.setText("取消订单");
        }
        if(order.getState().equals(OrderState.UNDELIVERED.toString()))
        {
            holder.btn_common.setVisibility(View.INVISIBLE);
            holder.btn_common_more.setVisibility(View.INVISIBLE);
        }
        if(order.getState().equals(OrderState.INDELIVERED.toString()))
        {
            holder.btn_common.setVisibility(View.VISIBLE);
            holder.btn_common.setText("确认收货");
            holder.btn_common_more.setVisibility(View.INVISIBLE);
        }
        if(order.getState().equals(OrderState.FINISH.toString()))
        {
            holder.btn_common.setVisibility(View.VISIBLE);
            holder.btn_common.setText("评价");
            holder.btn_common_more.setVisibility(View.VISIBLE);
            holder.btn_common_more.setText("申请退款");
        }

        holder.tv_name.setText(order.getGoodname());
        holder.tv_price.setText("商品价格："+order.getPrice());
        holder.tv_phone.setText("联系人电话："+order.getPhone());

        holder.btn_common.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(holder.btn_common.getText().equals("支付"))
                    checkInterface.checkGroup(position);
                if(holder.btn_common.getText().equals("确认收货"))
                    checkInterface.checkGroup2(position);
                if (holder.btn_common.getText().equals("评价")) {
                    checkInterface.checkGroup3(position);
                }

            }
        });

        holder.btn_common_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.btn_common_more.getText().equals("取消订单")) {
                    checkInterface.checkGroup1(position);
                }
                if (holder.btn_common_more.getText().equals("申请退款")) {
                    checkInterface.checkGroup4(position);
                }
            }
        });

        return convertView;
    }


    //初始化控件
    class ViewHolder {
        TextView tv_name , tv_price ,  tv_phone ;
        Button btn_common , btn_common_more;
        public ViewHolder(View itemView) {
            tv_name = itemView.findViewById(R.id.tv_commodity_ordername);
            tv_price = itemView.findViewById(R.id.tv_commodity_price);
            tv_phone = itemView.findViewById(R.id.tv_commodity_phone);
            btn_common = itemView.findViewById(R.id.btn_commmon);
            btn_common_more = itemView.findViewById(R.id.btn_commmon_more);
        }
    }

    /**
     * 复选框接口
     */
    public interface CheckInterface {
        /**
         * 付款
         * @param position  元素位置
         */
        void checkGroup(int position);

        /**
         * 取消订单
         * @param position
         */
        void checkGroup1(int position);

        /**
         * 确认收货
         */
        void checkGroup2(int position);

        /**
         * 评价
         * @param position
         */
        void checkGroup3(int position);

        /**
         * 申请退款
         * @param position
         */
        void checkGroup4(int position);
    }

}
